#include "../include/webPageOL.h"
#include <string>
#include "../include/WordSegmentation.h"
#include "../include/TextColor.h"
#include <algorithm>
#include <functional>
#include <iterator>
#include <string>


webPage::webPage()//无参构造函数
{}

webPage::webPage(const string & doc, WordSegmentation * jieba)
:_jieba(jieba)
 //有参构造函数
{
    processDoc(doc);
}

webPage::~webPage()//析构函数
{

}

int webPage::getDocid()//获取文章的docid
{
    return _docid;
}

string webPage::getTile()//获取文章的标题
{
    if(_docTitle.size()) {
        return _docTitle;
    }else{
        return nullptr;
    }
}

string webPage::getUrl()//获取文章的URL
{
    if(_docUrl.size()) {
        return _docUrl;
    }else{
        return nullptr;
    }
}
size_t webPage::nBytesCode(const char ch) {
    if (ch & (1 << 7)) {
        int nBytes = 1;
        for (int idx = 0; idx != 6; ++idx) {
            if (ch & (1 << (6 - idx))) {
                ++nBytes;
            }
            else {
                break;
            }
        }
        return nBytes;
    }
    return 1;
}

string webPage::summary(const vector<string> & queryWord)//文章概要
{
    Color c;
    for(auto &s:queryWord)
    {
        //
        
        int pos = _docContent.find(s);
        

        auto its = _docContent.begin()+pos;
        int sz = 0;
        while(its!=_docContent.end() && sz<500)
        {
            int n = nBytesCode(*its);
            its += n;
            sz +=n;
        }
        string result;
        if(pos!=-1)
        {
            result+=c.red;
            
            result+=s;
            result+=c.yellow;

            string line = string(_docContent.begin()+pos+s.size(),its);
            string newline;
            auto it = line.begin();
            
            int pos2 = -1;
            int ssz = 0;
            for(auto &ss:queryWord)
            {
                size_t pos = line.find(ss);
                if (pos != string::npos) {
                    string tmp = c.red + ss + c.yellow;
                    line.replace(pos, ss.length(), tmp);
                }
            }
            //第二个关键字的位置
            result+=line;
        }
        else{
            result = "NULL";
        }
        return result;
    }
    //string sum;
    /*int sz = 0;
    if(_docContent.size()<500)
    {
        string sum = _docContent.substr(0, sz);
        return sum;
    }
    auto its = _docContent.begin();
    sz = 0;

    /*while(its!=_docContent.end() && sz<500)
    {
        int n = nBytesCode(*its);
        its += n;
        sz +=n;
    }*/

    /*string sum = _docContent.substr(0, sz);
    return sum;*/
}


void webPage::processDoc(const string & doc)//对格式化文档进行处理
{
    
    size_t begin = doc.find("<docid>");
    size_t end = doc.find("</docid>");
    begin += 7;

    _docid = atoi(doc.substr(begin, end - begin).data());

    begin = doc.find("<title>");
    end = doc.find("</title>");
    begin += 7;
    _docTitle = doc.substr(begin, end - begin);

    begin = doc.find("<url>");
    end = doc.find("</url>");
    begin += 5;
    _docUrl = doc.substr(begin, end - begin);

    begin = doc.find("<content>");
    end = doc.find("</content>");
    begin += 9;
    _docContent = doc.substr(begin, end - begin);
    
}

map<string, int> & webPage::getWordsMap()//获取文档的词频统计map
{
    string str = _docContent;
    vector<string> results = _jieba->operator()(str);
    for(string & word : results) {
        int & isExit = _wordsMap[word];
        if (isExit){
            ++isExit;
        } else {
            isExit = 1;
        }
    }
    return _wordsMap;
}


